package com.yaodian100.ec.campaign.web;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.campaign.entity.CampaignDiscount;
import com.yaodian100.ec.campaign.service.CampaignDiscountService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.common.web.AbstractAction;

/**
 * @author eric.chen
 * 
 */
public class CampaignDiscountAction extends AbstractAction {

	private static final long serialVersionUID = 178887580319281383L;

	@Autowired
	private CampaignDiscountService campaignDiscountService;
	@Autowired
	private MenuService menuService;

	/**
	 * 活动集合物件 Campaign Entity List
	 */
	private List<CampaignDiscount> campaignList;

	/**
	 * 活动集合物件 Campaign Entity
	 */
	private CampaignDiscount campaign;
	/**
	 * 物件识别码
	 */
	private String uuid;
	private String startDate;
	private String startTime;
	private String endDate;
	private String endTime;

	/* 搜寻条件 */
	private BigDecimal paramMinOrderAmount;
	private Date paramStartDateTime = new Date();
	private Date paramEndDateTime;
	private String[] paramEnable = new String[] { "true", "false" };

	/* 选单 */
	private Menu menuMemberRank;

	/**
	 * 載入新增活动資料頁
	 * 
	 * @return
	 */
	public String add() {
		campaign = new CampaignDiscount();
		Calendar cal = DateUtil.getToday();
		cal.add(Calendar.DATE, 7);
		cal.set(Calendar.HOUR_OF_DAY, 10);
		cal.set(Calendar.MINUTE, 0);
		campaign.setStartDateTime(cal.getTime());
		cal.add(Calendar.DATE, 7);
		campaign.setEndDateTime(cal.getTime());
		campaign.setAllowMemberRank(menuMemberRank.getOptions().keySet().iterator().next());

		return "edit";
	}

	public String delete() throws Exception {
		try {
			if (campaign == null) {
				addActionError("活动不存在");
				return search();
			}
			campaignDiscountService.delete(campaign);
		} catch (Exception e) {
			addActionError("删除失败：" + e.getMessage());
			e.printStackTrace();
			return "view";
		}
		return search();
	}

	/**
	 * 修改活动資料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		if (campaign == null) {
			addActionError("活动不存在, 重新选取");
			return search();
		}
		return "edit";
	}

	/**
	 * @return the campaign
	 */
	public CampaignDiscount getCampaign() {
		return campaign;
	}

	/**
	 * @return the campaignList
	 */
	public List<CampaignDiscount> getCampaignList() {
		return campaignList;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * @return the menuMemberRank
	 */
	public Menu getMenuMemberRank() {
		return menuMemberRank;
	}

	/**
	 * @return the paramEnable
	 */
	public String[] getParamEnable() {
		return paramEnable;
	}

	/**
	 * @return the paramEndDateTime
	 */
	public String getParamEndDateTime() {
		if (paramEndDateTime != null) {
			return DateUtil.convertDateToString(DateUtil.PATTERN_DATE3, paramEndDateTime);
		} else {
			return "";
		}
	}

	/**
	 * @return the paramMinOrderAmount
	 */
	public String getParamMinOrderAmount() {
		if (paramMinOrderAmount != null) {
			return paramMinOrderAmount.toString();
		} else {
			return "";
		}
	}

	/**
	 * @return the paramStartDateTime
	 */
	public String getParamStartDateTime() {
		if (paramStartDateTime != null) {
			return DateUtil.convertDateToString(DateUtil.PATTERN_DATE3, paramStartDateTime);
		} else {
			return "";
		}
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * @return the uuid
	 */
	public String getUuid() {
		return uuid;
	}

	public String list() throws Exception {
		return "list";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.common.web.AbstractAction#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		if (menuMemberRank == null) {
			menuMemberRank = menuService.get("Member.Rank");
		}

		BeanUtilsBean utils = new BeanUtilsBean();
		ConvertUtilsBean cu = utils.getConvertUtils();
		cu.register(new BigDecimalConverter(), BigDecimal.class);
	}

	/**
	 * 储存行销活动资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			if (campaign != null) {
				campaign.setStartDateTime(DateUtil.convertStringToDate(EcConstant.PATTERN_TIMESTAMP_HH_MM, startDate + " "
						+ startTime));

				campaign.setEndDateTime(DateUtil.convertStringToDate(EcConstant.PATTERN_TIMESTAMP_HH_MM, endDate + " "
						+ endTime));
				logger.debug("discount:{}", campaign.getDiscountData());
				campaign = campaignDiscountService.save(campaign);
			} else {
				addActionError("活动不存在");
				return search();
			}
		} catch (Exception e) {
			addActionError("储存失败：" + e.getMessage());
			e.printStackTrace();
			return "edit";
		}
		return "view";
	}

	public String search() throws Exception {
		try {
			CommonCriteria cri = new CommonCriteria();
			if (paramMinOrderAmount != null) {
				cri.addLe("minOrderAmount", paramMinOrderAmount);
			}
			if (paramStartDateTime != null) {
				cri.addGe("endDateTime", DateUtil.getFirstTimestamp(Calendar.DATE, paramStartDateTime));
			}
			if (paramEndDateTime != null) {
				cri.addLe("startDateTime", DateUtil.getLastTimestamp(Calendar.DATE, paramEndDateTime));
			}
			if (paramEnable != null && paramEnable.length == 1) {
				cri.addEq("enable", Boolean.parseBoolean(paramEnable[0]));
			}
			int entitySize = campaignDiscountService.getListSize(cri).intValue();
			getPager().setTotalSize(entitySize);
			campaignList = campaignDiscountService.getList(getPager().getCurrentPage() * getPager().getPageRecord(),
					getPager().getPageRecord(), cri, new String[] { "startDateTime desc", "minOrderAmount desc",
							"discountData desc", "uuid" });
		} catch (Exception e) {
			addActionError("搜寻失败：" + e.getMessage());
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	/**
	 * @param paramEnable the paramEnable to set
	 */
	public void setParamEnable(String[] paramEnable) {
		this.paramEnable = paramEnable;
	}

	/**
	 * @param paramEndDateTime the paramEndDateTime to set
	 */
	public void setParamEndDateTime(String paramEndDateTime) {
		if (StringUtils.isNotBlank(paramEndDateTime)) {
			this.paramEndDateTime = DateUtil.convertStringToDate(DateUtil.PATTERN_DATE3, paramEndDateTime);
		} else {
			this.paramEndDateTime = null;
		}
	}

	/**
	 * @param paramMinOrderAmount the paramMinOrderAmount to set
	 */
	public void setParamMinOrderAmount(String paramMinOrderAmount) {
		if (StringUtils.isNotBlank(paramMinOrderAmount)) {
			this.paramMinOrderAmount = new BigDecimal(paramMinOrderAmount).setScale(1, BigDecimal.ROUND_FLOOR);
		}
	}

	/**
	 * @param paramStartDateTime the paramStartDateTime to set
	 */
	public void setParamStartDateTime(String paramStartDateTime) {
		if (StringUtils.isNotBlank(paramStartDateTime)) {
			this.paramStartDateTime = DateUtil.convertStringToDate(DateUtil.PATTERN_DATE3, paramStartDateTime);
		} else {
			this.paramStartDateTime = null;
		}
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/**
	 * @param uuid the uuid to set
	 */
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	/**
	 * 檢視活动資料
	 * 
	 * @return
	 */
	public String view() throws Exception {
		if (StringUtils.isNotBlank(uuid)) {
			campaign = campaignDiscountService.get(uuid);
		}
		if (campaign == null) {
			addActionError("活动UUID 有误, 重新选取");
			return search();
		}
		return "view";
	}

}